package com.driving.single.common.redis.service.impl;

import com.driving.single.common.entity.RecommendResult;
import com.driving.single.common.redis.service.CommonRedisFrontendPortalService;
import com.driving.single.frontend.portal.dto.PortalAdDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class CommonRedisFrontendPortalServiceImpl implements CommonRedisFrontendPortalService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Value("${redis_key_frontend_portal_content}")
    private String redis_key_frontend_portal_content;
    @Value("${redis_key_frontend_portal_expire_second}")
    private Long redis_key_frontend_portal_expire_second;

    /**
     * 缓存首页大广告位
     * @param tbContents 首页大广告位对象
     * @param cid 内容分类ID
     */
    @Override
    public Integer insertRedisFrontendContentWithCid(List<PortalAdDTO> tbContents, Long cid) {
        String key = redis_key_frontend_portal_content + ":" + cid;
        this.redisTemplate.opsForValue().set(key, tbContents, redis_key_frontend_portal_expire_second, TimeUnit.SECONDS);
        return 200;
    }

    /**
     * 查询缓存中的首页大广告位
     */
    @Override
    public List<PortalAdDTO> selectRedisFrontendContentWithCid(Long cid) {
        String key = redis_key_frontend_portal_content + ":" + cid;
        return (List<PortalAdDTO>) this.redisTemplate.opsForValue().get(key);
    }

    /**
     * 查询缓存中的首页(驾校/教练/陪练)推荐位
     * @param cid 内容分类ID
     */
    @Override
    public List<RecommendResult> selectRedisRecommendContentWithCid(Long cid) {
        String key = redis_key_frontend_portal_content + ":" + cid;
        return (List<RecommendResult>) this.redisTemplate.opsForValue().get(key);
    }
    /**
     * 缓存首页(驾校/教练/陪练)推荐位
     * @param recommendResults 首页(驾校/教练/陪练)推荐位
     * @param cid 内容分类ID
     */
    @Override
    public Integer insertRedisRecommendContentWithCid(List<RecommendResult> recommendResults, Long cid) {
        String key = redis_key_frontend_portal_content + ":" + cid;
        this.redisTemplate.opsForValue().set(key, recommendResults, redis_key_frontend_portal_expire_second, TimeUnit.SECONDS);
        return 200;
    }
}
